package skiree.host.abetsy.controller;

import lombok.Data;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@CrossOrigin
public class ClientBasicController {

    @Value("${abetsy.user}")
    private String username;

    @Value("${abetsy.password}")
    private String password;

    @PostMapping("/api/private/v1/login")
    @ResponseBody
    public Object login(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        Map<String, Object> meta = new HashMap<>();
        if (user.getPassword().equals(password) && user.getUsername().equals(username)) {
            StpUtil.login("admin");
            data.put("email", "2940397985@qq.com");
            data.put("id", 101);
            data.put("rid", 0);
            data.put("token", StpUtil.getTokenValue());
            data.put("username", "admin");
            meta.put("msg", "登录成功");
            meta.put("status", 200);
        } else {
            meta.put("msg", "登录失败");
            meta.put("status", 505);
        }
        result.put("data", data);
        result.put("meta", meta);
        return result;
    }


    @GetMapping("/api/private/v1/menus")
    @ResponseBody
    public Object menus() {
        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String, Object>> result = new ArrayList<>();
        // 剧集管理
        Map<String, Object> animeMenus = new HashMap<>();
        animeMenus.put("id", 1);
        animeMenus.put("authName", "剧集管理");
        animeMenus.put("path", "anime");
        // 用户管理子集
        Map<String, Object> animeMenus1 = new HashMap<>();
        animeMenus1.put("id", 100);
        animeMenus1.put("authName", "剧集列表");
        animeMenus1.put("path", "anime");
        animeMenus.put("children", Collections.singletonList(animeMenus1));

        animeMenus.put("anime", 1);
        result.add(animeMenus);


        // 系统管理
        Map<String, Object> systemMenus = new HashMap<>();
        systemMenus.put("id", 2);
        systemMenus.put("authName", "系统管理");
        systemMenus.put("path", "system");
        // 用户管理子集
        Map<String, Object> systemMenus1 = new HashMap<>();
        systemMenus1.put("id", 200);
        systemMenus1.put("authName", "任务队列");
        systemMenus1.put("path", "eventQueue");

        Map<String, Object> systemMenus2 = new HashMap<>();
        systemMenus2.put("id", 200);
        systemMenus2.put("authName", "系统设置");
        systemMenus2.put("path", "system");

        systemMenus.put("children", Arrays.asList(systemMenus1, systemMenus2));
        systemMenus.put("system", 2);
        result.add(systemMenus);


        resultMap.put("data", result);
        Map<String, Object> meta = new HashMap<>();
        meta.put("msg", "获取菜单列表成功");
        meta.put("status", 200);
        resultMap.put("meta", meta);
        return resultMap;
    }

    @Data
    private static class User {
        private String password;
        private String username;
    }

}
